package com.jade.shoesmallinterface.controller;

import com.jade.shoesmallinterface.entity.Result;
import com.jade.shoesmallinterface.entity.User;
import com.jade.shoesmallinterface.entity.dto.UserLoginDto;
import com.jade.shoesmallinterface.service.UserService;
import com.jade.shoesmallinterface.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoes/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    @Resource
    private UserService userService;

    /**
     * 查询所有用户信息
     * @return 用户信息列表
     */
    @GetMapping("/selectAllUsers")
    public Result<List<User>> selectAllUsers() {

            List<User> allUser= userService.selectAllUsers();

            return Result.success(allUser);

    }

    /**
     * 用户登录接口
     * @param*2 username password 用户名,密码
     * @return 包含token的响应结果
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLoginDto userLoginDto) {
            // 根据邮箱查询用户信息
            User user = userService.selectUserInfoByEmail(userLoginDto.getEmail());
            if (user == null) {
                return Result.error("用户不存在");
            }
            // 验证密码
            boolean isPasswordCorrect = com.jade.shoesmallinterface.utils.Md5Util.checkPassword(userLoginDto.getPassword(), user.getPassword());
            if (!isPasswordCorrect) {
                return Result.error("密码错误");
            }
            // 生成token
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", user.getUserid());
            claims.put("username", user.getUsername());
            // 生成token
            String token = JwtUtil.genToken(claims);
            return Result.success(token);

    }
    /**
     * 根据邮箱查询用户信息
     * @return 用户信息
     */
    @GetMapping("/selectUserInfoByEmail")
    public Result<User> selectUserInfoByEmail( String userEmail){
        if (userEmail==null){
            return  Result.error("用户邮箱不能为空");
        }
        User result=userService.selectUserInfoByEmail(userEmail);
        return Result.success(result);
    }

    /**
     * 插入用户信息
     * @param user 用户实体对象
     * @return 包含token的响应结果
     */
    @PostMapping("/insertUserInfo")
    public Result<Integer> insertUserInfo(@RequestBody User user) {
        System.out.println("用户信息："+user);
        if (user==null){
            return Result.error("插入用户信息不能为空");
        }
        int result=userService.insertUserInfo(user);
        return Result.success(result);
    }

    /**
     * 根据用户名删除用户信息
     * @param username 用户名
     * @return 删除成功的记录数
     */
    @DeleteMapping("/deleteUserByName")
    public Result<Integer> deleteUserByName(@RequestParam String username) {
        if (username==null){
            return Result.error("用户名名称不能为空");
        }
        int result = userService.deleteUserByName(username);
        if (result==1){
            return Result.success(result);
        }
        return Result.error("删除失败");
    }

    /**
     * 更新用户信息
     * @param user 用户实体对象
     * @return 更新成功的记录数
     */
    @PutMapping("/updateUserInfo")
    public Result<Integer> updateUserInfo(@RequestBody User user) {
        int result=userService.updateUserInfo(user);
        return Result.success(result);
    }
}
